
package cz.muni.fi.pa165.rentalofconstructionmachinery.service.impl;

import cz.muni.fi.pa165.rentalofconstructionmachinery.dao.MachineDAO;
import cz.muni.fi.pa165.rentalofconstructionmachinery.dto.MachineDTO;
import cz.muni.fi.pa165.rentalofconstructionmachinery.dto.converters.MachineDTOConverter;
import cz.muni.fi.pa165.rentalofconstructionmachinery.entity.Machine;
import cz.muni.fi.pa165.rentalofconstructionmachinery.service.MachineService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Zuzana Krejcova
 */
@Service("machineService")
@Transactional
public class MachineServiceImpl implements MachineService {

    @Autowired
    private MachineDAO machineDao;
    
    @Override
    public void addMachine(MachineDTO machine) {
        machineDao.createMachine(MachineDTOConverter.dtoToEntity(machine));
    }

    @Override
    public List<MachineDTO> listMachines() {
        List<MachineDTO> machines = new ArrayList<>();
        for (Machine machine : machineDao.getAllMachines()) {
            machines.add(MachineDTOConverter.entityToDTO(machine));
        }
        return machines;
    }

    @Override
    public MachineDTO searchForMachineByPlate(String plate) {
        MachineDTO machine = null;
        for (Machine m : machineDao.getAllMachines()) {
            if (m.getLicensePlate().equals(plate)) {
                machine = MachineDTOConverter.entityToDTO(m);
            }
        }
        return machine;
    }

    @Override
    public MachineDTO searchForMachineById(Long id) {
        return MachineDTOConverter.entityToDTO(machineDao.getMachineById(id));
    }
    
    @Override
    public void modifyMachine(MachineDTO machine) {
        machineDao.updateMachine(MachineDTOConverter.dtoToEntity(machine));
    }

    @Override
    public void removeMachine(MachineDTO machine) {
        machineDao.deleteMachine(MachineDTOConverter.dtoToEntity(machine));
    }
}
